package cn.com.mall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.com.mall.product.entity.AttrAttrgroupRelationEntity;
import cn.com.mall.product.entity.AttrEntity;
import cn.com.mall.product.service.AttrAttrgroupRelationService;
import cn.com.mall.product.service.AttrService;
import cn.com.mall.product.service.CategoryService;
import cn.com.mall.product.vo.AttrGroupWithAttrsVo;
import cn.com.mall.product.vo.AttrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.com.mall.product.entity.AttrGroupEntity;
import cn.com.mall.product.service.AttrGroupService;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.R;



/**
 * 属性分组
 *
 * @author yangwen
 * @email yangwen@servyou.com.cn
 * @date 2020-04-09 23:52:34
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrRelation;

    //  http://localhost:88/api/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationList){
        attrRelation.attrRelation(relationList);

        return R.ok();
    }

    /**
     * 删除属性和属性分组的对应关系
     * @param attrRelationList
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody List<AttrVO> attrRelationList){
        attrRelation.deleteRelation(attrRelationList);

        return R.ok();
    }

//    http://localhost:88/api/product/attrgroup/9/noattr/relation?t=1596591525840&page=1&limit=10&key=
//    /product/attrgroup/{attrgroupId}/noattr/relation

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") String attrgroupId){

        PageUtils page=attrService.noattrRelation(params,attrgroupId);

        return R.ok().put("page",page);
    }

    /**
     * 获取所有的属性和分组的关联关系
     * @param attrgroupId
     * @return
     */
    //    http://localhost:88/api/product/attrgroup/9/attr/relation?t=1596380673177
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> entities=attrService.getRelationAttr(attrgroupId);

        return R.ok().put("data", entities);

    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") int catelogId){
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId=attrGroup.getCatelogId();
        //根据catelogId查询出来完整路径
        Long[] path=categoryService.findCatelogPath(catelogId);

        attrGroup.setCatePath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

//    http://localhost:88/api/product/attrgroup/225/withattr?t=1601360736841
    @RequestMapping("/{catlogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catlogId") Long catLogId ){

     // 根据catlogId获取到所有的分组信息
        List<AttrGroupEntity>  attrGroups=attrGroupService.getAttrGroupsByCatlogId(catLogId);
        if(attrGroups!=null){

            List<AttrGroupWithAttrsVo> collect = attrGroups.stream().map(item -> {

                        AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();

                        BeanUtils.copyProperties(item, attrGroupWithAttrsVo);

                        Long attrGroupId = item.getAttrGroupId();
                        List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupId);

                        if(relationAttr==null){
                           return attrGroupWithAttrsVo;
                        }
                        List<AttrVO> attrVOS = relationAttr.stream().map(attrEntity -> {
                            AttrVO attrVO = new AttrVO();
                            BeanUtils.copyProperties(attrEntity, attrVO);

                            return attrVO;
                        }).collect(Collectors.toList());

                        attrGroupWithAttrsVo.setAttrs(attrVOS);

                        return attrGroupWithAttrsVo;

             }

            ).collect(Collectors.toList());

            return R.ok().put("data",collect);

        }

        return R.ok().put("data",null);
    }

}
